package my.ezbart;

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.UIBarButtonItem;
import org.xmlvm.iphone.UIBarButtonItemDelegate;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIDatePicker;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UISegmentedControl;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * SelectTime.java
 * 
 * View Controller that allows the user to select what time they will be arriving at or departing from
 * their selected stations. 
 * 
 * Holds a Segmented Control that acts as a radio button for arrival or departure. Also contains a Date Picker
 * for the user to select their preferred time and date.
 */
public class SelectTime extends UIViewController {
	private UIView root = new UIView(UIScreen.mainScreen().getApplicationFrame());
	private UIBarButtonItem doneButton = new UIBarButtonItem("Done", 2, new BarButtonDelegate());
	final private UIDatePicker dp = new UIDatePicker();
	final private UISegmentedControl arriveDepartButton = new UISegmentedControl();
	
    public SelectTime() {
    	
    	this.setTitle("Select Time");	
    }
    
    public class BarButtonDelegate implements UIBarButtonItemDelegate {
    	
    	public void clicked() {
        	UIViewController tp = getNavigationController().getViewControllers().get(0);
        	int tripType = arriveDepartButton.getSelectedSegmentIndex(); //0 = departure, 1 = arrival
        	DateParser dateParser = new DateParser(dp.getDate());
        	String timeOutput;
        	
        	timeOutput = dateParser.getDate();
        	
        	if (tripType == 0)
        		timeOutput = "Depart By - " + timeOutput;
        	if (tripType == 1)
        		timeOutput = "Arrive By - " + timeOutput;
        	
        	((TripPlanner)tp).setOptions(timeOutput, 2);
        	
            getNavigationController().popViewControllerAnimated(true);
		}
    }
    
    @Override
    public void loadView() {
    	root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
    	
    	arriveDepartButton.setFrame(new CGRect(40, 60, UIScreen.mainScreen().getApplicationFrame().size.width-80, 40));
    	arriveDepartButton.insertSegmentWithTitle("Depart By", 0, false);
    	arriveDepartButton.insertSegmentWithTitle("Arrive By", 1, false);
    	arriveDepartButton.setSelectedSegmentIndex(0);

    	dp.setMinuteInterval(15); 
    	dp.setDatePickerMode(2);
    	dp.setFrame(new CGRect(0, UIScreen.mainScreen().getApplicationFrame().size.height-(dp.getFrame().size.height+90), 
    			UIScreen.mainScreen().getApplicationFrame().size.width, dp.getFrame().size.height));
    	
    	doneButton.setEnabled(true);
    	this.getNavigationItem().setRightBarButtonItem(doneButton);
    	
    	root.addSubview(arriveDepartButton);
    	root.addSubview(dp);
    	
    	this.setView(root);
    }
}